package com.filepublic.demo.common.util;

import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.spring.web.json.Json;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.UUID;

public class FileUtil {

    /**
     * 读取指定文件为二进制
     * @param file
     * @return
     * @throws IOException
     */
    public static byte[] readFileBytes(File file) throws IOException {
        byte[] bytes = new byte[(int)file.length()];

        FileInputStream fileInputStream = new FileInputStream(file);

        int read = fileInputStream.read(bytes);
        System.out.println("读取字节数："+read);

        fileInputStream.close();

        return bytes;
    }

    /**
     * 生成新的文件名
     * @param file
     * @return
     */
    public static String buildNewFileName(MultipartFile file){
        //获取后缀名
        String originalFilename = file.getOriginalFilename();
        int lastIndexOf = originalFilename.lastIndexOf(".");
        String suffix = originalFilename.substring(lastIndexOf);

        //生成新的文件名
        String fileName = UUID.randomUUID().toString();

        return fileName+suffix;
    }
}











